package it.clem.little.domain.units;

import it.clem.little.domain.ArmorType;
import it.clem.little.domain.AttackType;
import it.clem.little.domain.BuildCost;
import it.clem.little.domain.UnitSize;

public class BaseUnit implements IUnit {
  private final String name;
  private final ArmorType armorType;
  private final AttackType attackType;
  private final int armorPoints;
  private final int attackPoints;
  private final int range;
  private final BuildCost buildCost;
  private final int population;
  private final UnitSize size;
  private final int initialHP;
  private final int attackInterval;
  private int currentHP;
  

  public BaseUnit(String name, ArmorType armorType, AttackType attackType, int armorPoints, int attackPoints, int range, BuildCost buildCost, int population,
      UnitSize size, int initialHP,int attackInterval) {
    super();
    this.name = name;
    this.armorType = armorType;
    this.attackType = attackType;
    this.armorPoints = armorPoints;
    this.attackPoints = attackPoints;
    this.range = range;
    this.buildCost = buildCost;
    this.population = population;
    this.size = size;
    this.initialHP = initialHP;
    this.currentHP = initialHP;
    this.attackInterval = attackInterval;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public ArmorType getArmorType() {
    return armorType;
  }

  @Override
  public AttackType getAttackType() {
    return attackType;
  }

  @Override
  public int getArmorPoints() {
    return armorPoints;
  }

  @Override
  public int getAttackPoints() {
    return attackPoints;
  }

  @Override
  public int getRange() {
    return range;
  }

  @Override
  public BuildCost getBuildCost() {
    return buildCost;
  }

  @Override
  public int getPopulation() {
    return population;
  }

  @Override
  public UnitSize getSize() {
    return size;
  }

  @Override
  public int getInitialHP() {
    return initialHP;
  }

  @Override
  public int getCurrentHP() {
    return currentHP;
  }

  @Override
  public boolean isDead() {
    return currentHP <= 0;
  }

  @Override
  public void addHP(int hp) {
    currentHP = Math.min(initialHP, currentHP + hp);
  }

  @Override
  public void subtractHP(int hp) {
    currentHP = Math.max(currentHP, currentHP - hp);
  }

  @Override
  public boolean isNonCombatant() {
    return attackPoints == 0;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + currentHP;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    BaseUnit other = (BaseUnit) obj;
    if (currentHP != other.currentHP)
      return false;
    if (name == null) {
      if (other.name != null)
        return false;
    } else if (!name.equals(other.name))
      return false;
    return true;
  }

  @Override
  public int getAttackInterval() {
    return attackInterval;
  }

}
